const xlsx = require('xlsx');

const generateExcel = (data) => {
  // 将查询到的 JSON 数据转换为 Excel 格式
  const worksheet = xlsx.utils.json_to_sheet(data);
  const workbook = xlsx.utils.book_new();
  xlsx.utils.book_append_sheet(workbook, worksheet, 'Orders');

  // 生成 Excel 文件的二进制缓冲区
  const excelBuffer = xlsx.write(workbook, { bookType: 'xlsx', type: 'buffer' });
  return excelBuffer;
};

module.exports = { generateExcel };
